package com.example.tagsdemo.directive;

import freemarker.core.Environment;
import freemarker.template.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class MyTagDirective implements TemplateDirectiveModel {

    private  static final String PAGESIZE = "pageSize";
    private static final String METHOD_KEY = "method";
    /**
     *
     * @param environment 运行的环境
     * @param map 方法参数map  方法名和值
     * @param templateModels
     * @param templateDirectiveBody 输出形式
     * @throws TemplateException
     * @throws IOException
     */
    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
        Integer tagId = -1;
        String tagName = "";

        //自己的逻辑 todo
        Map<String, Object> result = new HashMap<String, Object>(2);
        if (map.containsKey("tagId")){
            tagId = Integer.parseInt(map.get("tagId").toString());
        }

        if (map.containsKey("tagName")){
            tagName = (String)map.get("tagName").toString();
        }

        result.put("tagId", tagId);
        result.put("tagName", tagName);

        DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
        environment.setVariable("myTag", builder.build().wrap(result));
        templateDirectiveBody.render(environment.getOut());
    }
}
